package com.soft.base.enums;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.soft.base.exception.ParamException;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;
/**
 * class
 *
 * @author user
 * @date 2020/06/05
 */
public class ValueNameSerializer implements ObjectSerializer {
    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        if (object instanceof ValueNameEnum) {
            serializer.out.write(JSON.toJSONString(((ValueNameEnum) object).getValue()));
        } else if (object instanceof List) {
            serializer.out.write('[');
            boolean commanate = false;
            for (Object o : ((List) object)) {
                if (commanate) {
                    serializer.out.write(',');
                }
                serializer.out.write(JSON.toJSONString(((ValueNameEnum) o).getValue()));
                commanate = true;
            }
            serializer.out.write(']');
        } else {
            ParamException.throwException("ValueNameSerializer.error");
        }
    }
}
